package com.suisung.shopsuite.account.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.suisung.shopsuite.account.model.entity.UserAttestation;
import com.suisung.shopsuite.account.model.req.UserAttestationReq;
import com.suisung.shopsuite.account.service.UserAttestationService;
import com.suisung.shopsuite.common.utils.CheckUtil;
import com.suisung.shopsuite.common.utils.ContextUtil;
import com.suisung.shopsuite.core.consts.Constants;
import com.suisung.shopsuite.core.web.CommonRes;
import com.suisung.shopsuite.core.web.controller.BaseController;
import com.suisung.shopsuite.core.web.model.BaseOrder;
import com.suisung.shopsuite.core.web.model.res.BaseListRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "用户实名表-平台")
@RestController
@RequestMapping("/manage/account/UserAttestation")
public class UserAttestationController extends BaseController {

    @Resource
    private UserAttestationService userAttestationService;

//    @PreAuthorize("hasAuthority('/manage/account/UserAttestation/list')")
    @ApiOperation(value = "用户实名表-分页列表查询", notes = "用户实名表-分页列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonRes<BaseListRes<UserAttestation>> list(UserAttestationReq userAttestationReq) {

        Integer userId = ContextUtil.checkLoginUserId();
        userAttestationReq.setUserId(userId);

        //额外排序
        BaseOrder baseOrder = new BaseOrder();
        baseOrder.setSidx("create_time");
        baseOrder.setSort(Constants.ORDER_BY_DESC);

        List<BaseOrder> order = new ArrayList<>();
        order.add(baseOrder);
        userAttestationReq.setOrder(order);

        IPage<UserAttestation> pageList = userAttestationService.lists(userAttestationReq);

        return success(pageList);
    }

//    @PreAuthorize("hasAuthority('/manage/account/UserAttestation/detail')")
    @ApiOperation(value = "用户实名表-通过attestationId查询", notes = "用户实名表-通过attestationId查询")
    @RequestMapping(value = "/{attestationId}", method = RequestMethod.GET)
    public CommonRes<?> get(@PathVariable Integer attestationId) {

        Integer userId = ContextUtil.checkLoginUserId();

        UserAttestation userAttestation = userAttestationService.get(attestationId);

        if (CheckUtil.checkDataRights(userId, userAttestation, UserAttestation::getUserId)) {
            return success(userAttestation);
        } else {

        }

        return fail();
    }

//    @PreAuthorize("hasAuthority('/manage/account/UserAttestation/add')")
    @ApiOperation(value = "用户实名表-添加", notes = "用户实名表-添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonRes<?> add(UserAttestationReq userAttestationReq) {

        Integer userId = ContextUtil.checkLoginUserId();
        UserAttestation userAttestation = BeanUtil.copyProperties(userAttestationReq, UserAttestation.class);
        userAttestation.setUserId(userId);
        userAttestation.setCreateTime(new Date());

        boolean success = userAttestationService.add(userAttestation);

        if (success) {
            return success();
        }

        return fail();
    }

//    @PreAuthorize("hasAuthority('/manage/account/UserAttestation/edit')")
    @ApiOperation(value = "用户实名表-编辑", notes = "用户实名表-编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public CommonRes<?> edit(UserAttestationReq userAttestationReq) {

        boolean success = true;
        Integer userId = ContextUtil.checkLoginUserId();

        UserAttestation userAttestation = BeanUtil.copyProperties(userAttestationReq, UserAttestation.class);
        userAttestation.setUserId(userId);
        userAttestation.setUpdateTime(new Date());

        UserAttestation attestation = userAttestationService.get(userAttestation.getAttestationId());

        if (CheckUtil.checkDataRights(userId, attestation, UserAttestation::getUserId)) {
            success = userAttestationService.saveUserAttestation(userAttestation);
        } else {
            success = false;
        }

        if (success) {
            return success(userAttestation);
        }

        return fail();
    }

//    @PreAuthorize("hasAuthority('/manage/account/UserAttestation/remove')")
    @ApiOperation(value = "用户实名表-通过attestationId删除", notes = "用户实名表-通过attestationId删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public CommonRes<?> remove(@RequestParam("attestationId") Integer attestationId) {

        boolean success = true;
        Integer userId = ContextUtil.checkLoginUserId();

        UserAttestation userAttestation = userAttestationService.get(attestationId);

        if (CheckUtil.checkDataRights(userId, userAttestation, UserAttestation::getUserId)) {
            success = userAttestationService.remove(attestationId);
        } else {
            success = false;
        }

        if (success) {
            return success();
        }

        return fail();
    }

//    @PreAuthorize("hasAuthority('/manage/account/UserAttestation/removeBatch')")
    @ApiOperation(value = "用户实名表-批量删除", notes = "用户实名表-批量删除")
    @RequestMapping(value = "/removeBatch", method = RequestMethod.POST)
    public CommonRes<?> removeBatch(@RequestParam("attestationId") String attestationIds) {
        boolean success = userAttestationService.remove(Convert.toList(Integer.class, attestationIds));

        if (success) {
            return success();
        }

        return fail();
    }
}
